package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import service.UserService;

/**
 * Servlet implementation class UserServiceServlet
 */
@WebServlet("/UserServiceServlet")
public class LogInServiceServlet extends HttpServlet {

	@EJB
	UserService userService;
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LogInServiceServlet() {
		super();
		// TODO Auto-generated constructor stub

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			String userType = userService.logIn(
					request.getParameter("username"),
					request.getParameter("password"));

			if (userType.equalsIgnoreCase("Administrator")) {
				HttpSession session = request.getSession();

				session.setAttribute("savedUserName",
						request.getParameter("username"));

				session.setAttribute("savedUserType", userType);
				session.setAttribute("errorMessage", "");

				response.sendRedirect("userPages/userServlet");
			} else if (userType.equalsIgnoreCase("Director General")) {
				HttpSession session = request.getSession();

				session.setAttribute("savedUserName",
						request.getParameter("username"));

				session.setAttribute("savedUserType", userType);
				session.setAttribute("errorMessage", "");

				response.sendRedirect("directorGeneralPages/directorGeneralPage.jsp");
			} else if (userType.equalsIgnoreCase("Recruiter")) {
				HttpSession session = request.getSession();

				session.setAttribute("savedUserName",
						request.getParameter("username"));

				session.setAttribute("savedUserType", userType);
				session.setAttribute("errorMessage", "");

				response.sendRedirect("directorDepartamentPages/directorDepartamentPage.jsp");
			} else if (userType.equalsIgnoreCase("Director HR")) {
				HttpSession session = request.getSession();

				session.setAttribute("savedUserName",
						request.getParameter("username"));

				session.setAttribute("savedUserType", userType);
				session.setAttribute("errorMessage", "");

				response.sendRedirect("directorDepartamentPages/directorDepartamentPage.jsp");
			} else if (userType.equalsIgnoreCase("Director Departament")) {
				HttpSession session = request.getSession();

				session.setAttribute("savedUserName",
						request.getParameter("username"));

				session.setAttribute("savedUserType", userType);
				session.setAttribute("errorMessage", "");

				response.sendRedirect("directorDepartamentPages/directorDepartamentPage.jsp");

			} else {
				HttpSession session = request.getSession();
				session.setAttribute("errorMessage", "error");

			}
		} catch (Exception e) {
			HttpSession session = request.getSession();
			session.setAttribute("errorMessage", "error");
			response.sendRedirect("login.jsp");
		}
	}

}
